package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zzyl.nursing.service.WechatService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Objects;

@Service
public class WechatServiceImpl implements WechatService {

    // 微信API_获取OpenId_地址
    private static final String GET_OPENID_URL = "https://api.weixin.qq.com/sns/jscode2session";
    // 获取token
    private static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
    // 获取手机号
    private static final String GET_PHONE_URL = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appid;
    @Value("${wechat.appSecret}")
    private String appSecret;

    /**
     * 获取微信OpenId
     *
     * @param code 登录时获取的code,通过小程序的wx.login获取
     * @return
     */
    @Override
    public String getOpenid(String code) {
        // 封装请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("appid", appid);
        paramMap.put("secret", appSecret);
        paramMap.put("js_code", code);
        paramMap.put("grant_type", "authorization_code");

        // 使用httpclient 发送请求,返回结果是json字符串
        String jsonResult = HttpUtil.get(GET_OPENID_URL, paramMap);
        // 使用jsonutil,将json字符串转换成对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonResult);
        // 判断接口响应是否出错
        if (ObjectUtil.isNotEmpty(jsonObject.getInt("errcode"))) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }
        // 返回openid
        return jsonObject.getStr("openid");
    }

    /**
     * 获取微信手机号
     *
     * @param detailCode
     * @return
     */
    @Override
    public String getPhone(String detailCode) {
        // 构造请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
//        paramMap.put("access_token", getToken());
        paramMap.put("code", detailCode);

        // 使用httpclient发送请求,获取jsonstr类型的返回值
        String jsonResult = HttpUtil.post(GET_PHONE_URL + getToken(), JSONUtil.toJsonStr(paramMap));
        // jsonstr转json对象
        JSONObject jsonObject = JSONUtil.parseObj(jsonResult);
        // 判断接口响应是否出错
        if (jsonObject.getInt("errcode") != 0) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }
        // 返回手机号
        return jsonObject.getJSONObject("phone_info").getStr("phoneNumber");
    }

    /**
     * 获取微信Token
     *
     * @return
     */
    private String getToken() {
        // 构造请求参数
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("grant_type", "client_credential");
        paramMap.put("appid", appid);
        paramMap.put("secret", appSecret);
        // 发送httpclient请求,获取jsonstr返回值
        String jsonResult = HttpUtil.get(GET_TOKEN_URL, paramMap);

        // json转obj
        JSONObject jsonObject = JSONUtil.parseObj(jsonResult);
        // 判断接口响应是否出错
        if (ObjectUtil.isNotEmpty(jsonObject.get("errcode"))) {
            throw new RuntimeException(jsonObject.getStr("errmsg"));
        }
        // 返回token
        return jsonObject.getStr("access_token");
    }
}
